/**
 * 公共的listview 组件
 * Created by jinwangtong on 16/3/28.
 *                 <Spinner type={"Bounce"} color="#EE4000" />
                <Spinner type={"Wave"} color="#EE4000" />
                <Spinner type={"WanderingCubes"} color="#EE4000" />
                <Spinner type={"Pulse"} color="#EE4000" />
                <Spinner type={"ChasingDots"} color="#EE4000" />
                <Spinner type={"ThreeBounce"} color="#EE4000" />
                <Spinner type={"Circle"} color="#EE4000" />
                <Spinner type={"9CubeGrid"} color="#EE4000" />
                <Spinner type={"FadingCircle"} color="#EE4000" />
                <Spinner type={"FadingCircleAlt"} color="#EE4000" />
 */
import React, {Component} from 'react';
import {
    View,
    Text,
    FlatList,
    ScrollView,
    ActivityIndicatorIOS,
    Platform,
    PixelRatio
} from 'react-native';
import Tools from "../tools/tool.js"
import Spinner from "react-native-spinkit"

export default class MyListView extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            isRefreshing: false,
            loadingmore: true,
        }

    }
    componentDidMount() {
        this.setState({
            isRefreshing: false,
            loadingmore: true,
        })
    }
   
    componentWillReceiveProps(nextProps) {     
        this.setState({
            isRefreshing: false,
        })
    }
    renderFooter() {
        if (this.props.dataSize >= this.props.count) {
            return (
                <View style={{justifyContent:"center",alignItems:"center",paddingVertical:5}}>
                    <Text style={{fontSize:14,color:"#808080"}}>{this.props.hideToast?"":this.props.moreToastText?this.props.moreToastText:"没有更多数据"}</Text>
                </View>
            )
        } else if (this.props.data&&this.props.data.length>0&&this.props.loadMore) {
            return (
                <View style={[{justifyContent:"center",alignItems:"center",paddingVertical:5,flexDirection:"row"}]}>
                     <Spinner type={"ThreeBounce"} color="#CD4F39" style={{marginTop:-8}} size={40}/>
                     <Text style={{fontSize:14,marginLeft:5,color:"#CD4F39"}}>正在加载中</Text>    
                </View>
            )
        }    
        return null;
    }

    render() {
         return(<FlatList
                    {...this.props}
                     refreshControl={this.props.showRefresh&&this.props.showRefresh=="false"?null:
                        Tools.intiRefresh(this.state.isRefreshing,this._onRefresh.bind(this))}
                    ListHeaderComponent={()=>{return this.props.renderHeader?this.props.renderHeader():null}}
                    ListFooterComponent={()=>this.renderFooter()}
                    onEndReachedThreshold={10}
                    initialListSize={this.props.dataSize}
                    onEndReached={this.loadMore.bind(this)}
                />)
    }

    _onRefresh() {
        if(this.props.onRefresh){
            this.setState({
                isRefreshing:true,
            });
            this.props.onRefresh();
        }
        this.props.scrollTop&&this.props.scrollTop()
    }

    /**
     * 加载更多
     */
    loadMore() {
        if(this.props.loadMore){
            if (this.props.dataSize < this.props.count) {
                this.props.loadMore()
            }
        }
    }
}
;